package bmatch.tech.todo.resource;

import bmatch.tech.todo.model.LoginUser;
import bmatch.tech.todo.model.Response;
import bmatch.tech.todo.model.User;
import bmatch.tech.todo.service.UserService;
import bmatch.tech.todo.util.FormatUtils;
import bmatch.tech.todo.util.JwtUtils;
import bmatch.tech.todo.util.PwdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.apache.commons.lang3.StringUtils;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.NewCookie;
import java.net.URI;

/**
 * @program:todo
 * @author:LinJinhao
 * @create:2018-12-27 23:48
 **/

@Component
@Path("/user")
public class UserResource {

    //用户服务
    private final UserService userService;

    @Autowired
    public UserResource(UserService userService) {
        this.userService = userService;
    }

    //邮箱注册账号
    @GET
    @Path("reg_email")
    @Produces(MediaType.APPLICATION_JSON)
    public Response regEmail(@DefaultValue("") @QueryParam("email") String email){
        Response response = new Response();

        //检查邮箱格式
        if (!FormatUtils.checkEmail(email) && StringUtils.isEmpty(email)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("邮箱格式不正确，请重新输入");

            return response;
        }

        //查询数据库是否存在该邮箱
        if (!userService.checkMailReg(email)) {
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("邮箱已注册，请重新输入另外一个邮箱");

            return response;
        }

        //发送激活码
        response.setMessage("激活码发送成功，请在5分钟内验证！");
        userService.createCdKey(email);

        return response;
    }

    //注册
    @POST
    @Path("register")
    @Produces(MediaType.APPLICATION_JSON)
    public Response register(@DefaultValue("") @QueryParam("email") String email,
                             @DefaultValue("") @QueryParam("pwd") String pwd,
                             @DefaultValue("") @QueryParam("second_pwd") String secondPwd,
                             @DefaultValue("") @QueryParam("cdkey") String cdKey){
        User user = new User();
        Response response = new Response();

        //检查邮箱格式
        if (!FormatUtils.checkEmail(email) && StringUtils.isEmpty(email)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("邮箱格式不正确，请重新输入");
            return response;
        }

        //测试密码数据
        //String pwd = "123456abc";
        //String secondPwd = "123456abc";
        //检查密码格式
        if (!FormatUtils.checkPwd(pwd)&&!FormatUtils.checkPwd(secondPwd)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("密码必须包含6-18位的字母、数字以及其他符号");
            return response;
        }

        //检查两次输入密码是否相同
        if (!pwd.equals(secondPwd)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("两次输入的密码不相同，请重新输入");

            return response;
        }

        //验证激活码
        if (cdKey.equals("")){
            response.setMessage("验证码不可为空，请输入");

            return response;
        }

        if (!userService.verifyCdKey(email,cdKey)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("验证码有误，请重新输入验证码或者重新发送验证码");
            /*if (temp.equal("yes")){
            userService.createCdKey(email);
            }*/
            return response;
        }
        user.setEmail(email);
        user.setPwd(pwd);

        LoginUser loginUser = userService.register(user);
        //将新创建的用户资料导入数据库
        if (loginUser != null){
            response.setStatusCode(Response.RESPONSE_SUCCESS);
            response.setMessage("注册成功");
            response.setOtherData(loginUser);

            return response;
        } else {
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("注册失败");

            return response;
        }
    }

    //登录
    @POST
    @Path("login")
    @Produces(MediaType.APPLICATION_JSON)
    public javax.ws.rs.core.Response Login(@DefaultValue("") @QueryParam("email") String email,
                                           @DefaultValue("") @QueryParam("pwd") String pwd){
        Response response = new Response();

        //检查邮箱格式
        if (!FormatUtils.checkEmail(email) && StringUtils.isEmpty(email)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("邮箱格式不正确，请重新输入");
            return javax.ws.rs.core.Response.accepted(response).build();
        }

        //使用加密的密码与数据库比较
        String encryptedPwd = PwdUtils.md5Password(pwd, "bmatch");
        LoginUser loginUser = userService.login(email,encryptedPwd);
        String userId = userService.getUserByEmail(email).getId();
        if (loginUser != null){
            //生成一个token
            String token = JwtUtils.generator(userId);

            response.setStatusCode(Response.RESPONSE_SUCCESS);
            response.setMessage("登录成功！");
            URI uri = URI.create("/main/get_user");
            return javax.ws.rs.core.Response
                    .seeOther(uri)
                    .cookie(new NewCookie("sid",token)).build();
        } else{
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("密码不正确，请重新输入");

            return javax.ws.rs.core.Response.accepted(response).build();
        }
    }

    //通过邮箱发送验证码
    @GET
    @Path("found")
    @Produces(MediaType.APPLICATION_JSON)
    public Response foundEmail(@DefaultValue("") @QueryParam("email") String email){
        Response response = new Response();

        //检查邮箱格式
        if (!FormatUtils.checkEmail(email) && StringUtils.isEmpty(email)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("邮箱格式不正确，请重新输入");

            return response;
        }

        //查询数据库是否存在该邮箱
        if (!userService.checkMailReg(email)) {
            response.setStatusCode(Response.RESPONSE_SUCCESS);
            //发送激活码
            response.setMessage("验证码发送成功，请在5分钟内验证！");
            userService.createCdKey(email);

            return response;
        }else {
            response.setStatusCode(Response.RESPONSE_FAIL);

            return response;
        }
    }

    //找回密码
    @GET
    @Path("found_pwd")
    @Produces(MediaType.APPLICATION_JSON)
    public Response foundPwd(@DefaultValue("") @QueryParam("email") String email,
                             @DefaultValue("") @QueryParam("cdkey") String cdKey,
                             @DefaultValue("") @QueryParam("pwd") String pwd){
        Response response = new Response();

        //检查邮箱格式
        if (!FormatUtils.checkEmail(email) && StringUtils.isEmpty(email)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("邮箱格式不正确，请重新输入");

            return response;
        }

        //验证密码格式
        if (!FormatUtils.checkPwd(pwd)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("密码必须包含6-18位的字母、数字以及其他符号");

            return response;
        }

        //验证激活码
        if (cdKey.equals("")){
            response.setMessage("验证码不可为空，请输入");

            return response;
        }

        if (!userService.verifyCdKey(email,cdKey)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("验证码有误，请重新输入验证码或者重新发送验证码");
            /*if (temp.equal("yes")){
            userService.createCdKey(email);
            }*/
            return response;
        }
        String userId = userService.getUserByEmail(email).getId();
        if (!userService.foundPwd(userId,pwd)){
            response.setStatusCode(Response.RESPONSE_FAIL);
            response.setMessage("修改密码失败!");

            return response;
        }
        response.setStatusCode(Response.RESPONSE_SUCCESS);
        response.setMessage("修改密码成功");

        return response;
    }
}
